from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
# from views.ui_waiting import *
import os

# 工具类

# 获取当前目录，/ \是区分mac和win目录格式区别
base_path = os.getcwd()
if not base_path.startswith('/'):
    base_path = base_path.replace('\\', '/')


# 字体
Bold_Font = QFont("PingFangSC-Semibold", 10)
Default_Font = QFont("PingFangSC-Regular", 10)
Default_Font_12 = QFont("PingFangSC-Regular", 10)

Inspect_Setting_Title_font = QFont("Roman times", 13, QFont.Bold)


# 设置窗体背景颜色，a 0为透明，255为不透明
def setBackColor(widget, r, g, b, a=255):
    palette = widget.palette()
    palette.setColor(QPalette.Background, QColor(r, g, b, a))
    widget.setPalette(palette)
    widget.setAutoFillBackground(True)


# 设置窗体背景图片
def setBackImage(widget, image_name):
    palette = widget.palette()
    palette.setBrush(QPalette.Background, QBrush(QPixmap(getPixmap(image_name))))
    widget.setPalette(palette)
    widget.setAutoFillBackground(True)


# 设置字体颜色
def setTextColor(widget, r, g, b, a=255):
    pe = widget.palette()
    pe.setColor(QPalette.WindowText, QColor(r, g, b, a))
    widget.setPalette(pe)


# 按钮设置图标
def setButtonIcon(button, icon_name, size=None):
    button.setStyleSheet('border:None')
    button.setIcon(getIcon(icon_name))
    if size is None:
        button.setIconSize(button.size())
    else:
        button.setIconSize(size)


# 给label设置pixmap
def setLabelImage(label, image_name):
    pixmap = getPixmap(image_name)
    label.setPixmap(pixmap)


# 返回一个图片label
def getImageLabel(image_name):
    pixmap = getPixmap(image_name)
    img_label = QLabel()
    img_label.setPixmap(pixmap)
    return img_label


# 返回一个颜色值的label
def getColorLabel(r, g, b, a=255):
    color_label = QLabel()
    setBackColor(color_label, r, g, b, a)
    return color_label


# 返回一个pixmap
def getPixmap(image_name):
    return QPixmap(base_path + '/resources/imgs/' + image_name)


# 返回icon
def getIcon(image_name):
    return QIcon(base_path + '/resources/imgs/' + image_name)


# 返回Image
def getImage(image_name):
    return QImage(base_path + '/resources/imgs/' + image_name)


# 检查报名信息中，数值处于报警数值的什么位置，0：正常，1：黄色警告，2：红色警告
def check_warning_value(state, value, value_list, label, label2=None):
    if ((value_list[0] is not None) and (value <= float(value_list[0]))) or \
            ((value_list[3] is not None) and (value >= float(value_list[3]))):
        setTextColor(label, 248, 70, 70)
        if label2 is not None:
            setTextColor(label2, 248, 70, 70)
        return 2
    if ((value_list[0] is not None) and (value_list[1] is not None)
       and (value < float(value_list[1])) and (value > float(value_list[0]))) or \
       ((value_list[2] is not None) and (value_list[3] is not None)
       and (value < float(value_list[3])) and (value > float(value_list[2]))):
        setTextColor(label, 255, 181, 0)
        if label2 is not None:
            setTextColor(label2, 255, 181, 0)
        if state == 2:
            return state
        return 1
    if (value_list[1] is not None) and (value_list[2] is not None) \
            and (value <= float(value_list[2])) and (value >= float(value_list[1])):
        setTextColor(label, 102, 102, 102)
        if label2 is not None:
            setTextColor(label2, 102, 102, 102)
        return state
    return state
